package com.taptech.doufu.db.offline;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.taptech.doufu.services.offline.OffLineInfoBean;

public class OffLineDataBaseUtil {
	private final String TABLE_NAME = OffLineDataBaseHelper.TABLE_NAME;
	private final String DB_NAME = OffLineDataBaseHelper.DBNAME;

	private OffLineDataBaseHelper helper;

	public OffLineDataBaseUtil(Context context) {
		helper = new OffLineDataBaseHelper(context, DB_NAME, null, 1);
	}

	// 添加记录
	public void insert(String offline_url, String offline_name,
			String offline_novelid, int offline_status, String offline_time) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO "
						+ TABLE_NAME
						+ " (offline_url,offline_name,offline_novelid,offline_status,offline_time) VALUES (?,?,?,?,?)",
				new Object[] { offline_url, offline_name, offline_novelid,
						offline_status, offline_time });
		db.close();
	}

	// 更新记录
	public void update(String offline_novelid, int offline_status) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("UPDATE " + TABLE_NAME
				+ " SET offline_status = ?  WHERE offline_novelid = ? ",
				new Object[] { offline_status, offline_novelid });
		db.close();
	}
	
	// 删除记录
	public void delete(String offline_novelid) {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("DELETE FROM " + TABLE_NAME + " WHERE offline_novelid = ? ",
				new Object[] { offline_novelid });
		db.close();
	}
	public void dropTable() {
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("DELETE FROM " + TABLE_NAME);
		db.close();
	}
	
	// 查询novelId
	public List<String> queryNovelId() {
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor c = db.rawQuery("SELECT offline_novelid FROM " + TABLE_NAME,
				null);
		List<String> novelIdList = new ArrayList<String>();
		while (c.moveToNext()) {
			if (c.getString(0) != null
					&& !c.getString(0).equalsIgnoreCase("null")) {
				novelIdList.add(c.getString(0));
			}
		}
		c.close();
		db.close();
		return novelIdList;
	}

}
